//
//  WHStatusPictureView.swift
//  WHSinaWeiBo
//
//  Created by 王寒 on 16/4/13.
//  Copyright © 2016年 wanghan. All rights reserved.
//

import UIKit
//算出固定值
private let pictureCellMargin: CGFloat = 3
let maxWidth = ScreenWidth - StatusCellMargin * 2
let itemWidth = (maxWidth - 2 * pictureCellMargin) / 3
private let pictureIden = "pictureCell"
class WHStatusPictureView: UICollectionView {

    
    var imageURls: [WHStatusPicture]?{
        didSet{
            print(imageURls?.count ?? 0)
            let size = caculatePictureViewSize()
            
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            //设置label数字
            textLabel.text = "\(imageURls?.count ?? 0)"
            
            //刷新
            self.reloadData()

        }
    }
    
    //一个图片显示100x150
    //四个图片显示2x2
    //其他3xN
    private func caculatePictureViewSize() -> CGSize{
        //图片数量
        let imageCount = imageURls?.count ?? 0

        //没有图片
        if imageCount == 0{
            return CGSizeZero
        }
        //如果是一个图片
        if imageCount == 1{
            //TODO
            return CGSize(width: 120, height: 180)
        }
        //四张图片
        if imageCount == 4{
            let fourWidth = 2 * itemWidth + pictureCellMargin
            print("四张图片 collection大小")
            print(fourWidth)
            return CGSize(width: fourWidth, height: fourWidth)

        }
        
        //其他情况
        /*
        123 (count - 1) /3 + 1
        456
        789
        */
        let row = CGFloat((imageCount - 1) / 3 + 1)
        let height = (row * itemWidth) + (row - 1) * pictureCellMargin
        return CGSize(width: maxWidth, height: height)

    }
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout = UICollectionViewFlowLayout()
        //设置flowLayout
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        flowLayout.minimumInteritemSpacing = pictureCellMargin
        flowLayout.minimumLineSpacing = pictureCellMargin 
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        //
        setupUI()
        //设置代理
        self.dataSource = self
        //注册cell
        self.registerClass(picutreCell.self, forCellWithReuseIdentifier: pictureIden)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        addSubview(textLabel)
        textLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
    }
    
    //图片数量label
    private var textLabel: UILabel = {
       let l = UILabel(text:"", textColor: UIColor.redColor(), fontSize: 13)
        return l
    }()
    
}
//MARK:数据源代理
extension WHStatusPictureView: UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        //创建
        //注册
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureIden, forIndexPath: indexPath) as! picutreCell
        
//        cell.backgroundColor = randomColor()
        cell.statusPirture = self.imageURls?[indexPath.row]
        
        return cell
    }
}

class picutreCell: UICollectionViewCell {
    
    var statusPirture: WHStatusPicture?{
        didSet{
            let url = NSURL(string: statusPirture?.thumbnail_pic ?? "")
            iconView.sd_setImageWithURL(url)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        steupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func steupUI(){
        addSubview(iconView)
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    
    private lazy var iconView: UIImageView = {
        let vi = UIImageView()
        //设置填充模式
        vi.contentMode = .ScaleAspectFill
        //手写代码需要设置裁剪
        vi.clipsToBounds = true
        return vi
    }()
}

